package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbee.smartcommunities.entity.RoleUser;
import com.fbee.smartcommunities.entity.User;
import com.fbee.smartcommunities.service.RoleUserService;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.vo.ResultVO;
import com.fbee.smartcommunities.vo.RoleAdminVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-17
 */
@RestController
@RequestMapping("/roleUser")
public class RoleUserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleUserService roleUserService;

    @GetMapping("/getData")
    public ResultVO<RoleAdminVO> getData(Long roleId){
        List<User> adminList = userService.list();
        List<User> roleAdminList = userService.list(new QueryWrapper<User>().eq("role_id", roleId));
        return ResultVO.success(new RoleAdminVO(adminList,roleAdminList));
    }

    @PostMapping("/save")
    public ResultVO<Boolean> save(Long roleId,Long[] userIds){
        // 在添加之前应该将有关数据全部删除
        return ResultVO.success(roleUserService.save(roleId,userIds),"添加成功");
    }

}
